package com.john.bluetoothtest2;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.john.bluetoothtest2.User_Info.userDB;


public class RegisterActivity extends AppCompatActivity {


    private long firstTime=0;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch(keyCode){
            case KeyEvent.KEYCODE_BACK://点击返回键
                long secondTime = System.currentTimeMillis();//以毫秒为单位
                if(secondTime -firstTime>2000){
                    Toast.makeText(this, "再按一次返回退出程序", Toast.LENGTH_SHORT).show();
                    firstTime=secondTime;
                }else{
                    finish();
                    System.exit(0);
                }
                return true;
        }
        return super.onKeyUp(keyCode, event);
    }






    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        Window window = getWindow();
        window.setStatusBarColor(ContextCompat.getColor(this, R.color.teal_200));
    }

    public void onclick_register(View view) {
        userDB userDB = new userDB(this);
        SQLiteDatabase db = userDB.getWritableDatabase();
        String account = ((EditText) findViewById(R.id.et3)).getText().toString();
        String password1 = ((EditText) findViewById(R.id.et4)).getText().toString();
        String password2 = ((EditText) findViewById(R.id.et5)).getText().toString();
        Cursor cursor = db.query("user_account", null, "phone = ?", new String[]{account}, null, null, null);
        if (cursor.getCount() != 0) {
            Toast.makeText(this, "该账号已经存在,请更改你的账号", Toast.LENGTH_SHORT).show();
        } else if (password1.equals(password2) == false) {
            Toast.makeText(this, "两次密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
        } else {
            db.execSQL("insert into user_account (phone,password) values(?,?)", new Object[]{
                    account, password1});
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        cursor.close();
        db.close();
    }


    public void esc(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}